/*******************************************************************************
 * File name: 		uart.c
 * Description: 	LPC1788's UART0 can work as a modem. UART4 has IrDA. There are
 * 					also standard UART0/2/3 available.
 * Project: 		_framework
 * Target: 			LPC1788
 * Compiler: 		arm-none-eabi-gcc
 * Date: 			08-11-2012
 * Author: 			Kuba
 * Based on: 		---
 ******************************************************************************/

/*==============================================================================
 Includes
==============================================================================*/
# include "LPC177x_8x.h"
# include "hdr_sc.h"
# include "macros.h"
# include "init.h"
# include "uart.h"

/*==============================================================================
 Defines
==============================================================================*/
# define UART_PORTS							5
# define UART0_BASE_DIV						16
# define UART1_BASE_DIV          			16
# define UART2_BASE_DIV          			16
# define UART3_BASE_DIV          			16

/* UART pins */
# define UART0_PIN_TXD						SET_BIT(0)
# define UART0_PIN_RXD						SET_BIT(0)
# define UART2_PIN_TXD						SET_BIT(0)
# define UART2_PIN_RXD						SET_BIT(0)

/* line control register */
# define LCR_WORD_LENGTH_SELECT_bit     	0
# define LCR_WORD_LENGTH_SELECT_5BIT    	SET_BITS(0, LCR_WORD_LENGTH_SELECT_bit)
# define LCR_WORD_LENGTH_SELECT_6BIT    	SET_BITS(1, LCR_WORD_LENGTH_SELECT_bit)
# define LCR_WORD_LENGTH_SELECT_7BIT 		SET_BITS(2, LCR_WORD_LENGTH_SELECT_bit)
# define LCR_WORD_LENGTH_SELECT_8BIT		SET_BITS(3, LCR_WORD_LENGTH_SELECT_bit)
# define LCR_STOP_BIT_SELECT_bit			2
# define LCR_STOP_BIT_SELECT_1BIT       	SET_BITS(0, LCR_STOP_BIT_SELECT_bit)
# define LCR_STOP_BIT_SELECT_2BIT       	SET_BITS(1, LCR_STOP_BIT_SELECT_bit)
# define LCR_BREAK_CONTROL              	SET_BIT(6)
# define LCR_DLAB                       	SET_BIT(7)

/* fifo control register */
# define FCR_FIFO_ENABLE              		SET_BIT(0)
# define FCR_FIFO_RST_RX                	SET_BIT(1)
# define FCR_FIFO_RST_TX                	SET_BIT(2)
# define FCR_DMA_ENABLE						SET_BIT(3)
# define FCR_RX_FIFO_TRIGGER_LEVEL_bit		6
# define FCR_RX_FIFO_TRIGGER_LVL_1CHAR		SET_BITS(0, FCR_RX_FIFO_TRIGGER_LEVEL_bit)
# define FCR_RX_FIFO_TRIGGER_LVL_4CHAR		SET_BITS(1, FCR_RX_FIFO_TRIGGER_LEVEL_bit)
# define FCR_RX_FIFO_TRIGGER_LVL_8CHAR  	SET_BITS(2, FCR_RX_FIFO_TRIGGER_LEVEL_bit)
# define FCR_RX_FIFO_TRIGGER_LVL_14CHAR 	SET_BITS(3, FCR_RX_FIFO_TRIGGER_LEVEL_bit)

/* interrupts enable register */
# define IER_INTERRUPT_EN_RBR				SET_BIT(0)
# define IER_INTERRUPT_EN_THRE          	SET_BIT(1)

/* line status register */
# define LSR_RECEIVER_DATA_READY   			SET_BIT(0)
# define LSR_OVERRUN_ERROR           		SET_BIT(1)
# define LSR_PARITY_ERROR             		SET_BIT(2)
# define LSR_FRAMING_ERROR            		SET_BIT(3)
# define LSR_BREAK_INTERRUPT          		SET_BIT(4)
# define LSR_THR_EMPTY                		SET_BIT(5)
# define LSR_TRANSMITTER_EMPTY        		SET_BIT(6)
# define LSR_RX_FIFO_ERROR            		SET_BIT(7)

/*==============================================================================
 Types
==============================================================================*/

/*==============================================================================
 Globals
==============================================================================*/

/*==============================================================================
 Static function prototypes
==============================================================================*/

/*==============================================================================
 Global function definitions
==============================================================================*/
/*------------------------------------------------------------------------------
 function name:		uart0Init
 description: 		init uart 0
 parameters:   		baud
 returned value:	none
------------------------------------------------------------------------------*/
void uart0Init    (enum uartBaud baud)
{
	uint32 div;

	/* turn on UART0 */
	LPC_SC->PCONP |= LPC_SC_PCONP_PCUART0;
    /* set DLAB to enable access to divisor latches- DLL and DLM are accessible,
     * 8 bit character, 1 bit stop, none parity */
    LPC_UART0->LCR = LCR_WORD_LENGTH_SELECT_8BIT /*| uartConf->stopBit | uartConf->parity*/ | LCR_DLAB;
    /* set divisor latches DLL, DLM */
    div = (SYSTEM_FREQUENCY / UART0_BASE_DIV) / baud;
    LPC_UART0->DLM = div / 256;
    LPC_UART0->DLL = div % 256;
    /* clear DLAB to disable DLL, DLM- now RBR, THR and IER are accessible */
    LPC_UART0->LCR &= ~LCR_DLAB;

    LPC_IOCON->P0_2 = 0;									/* clear pins */
    LPC_IOCON->P0_3 = 0;
    LPC_IOCON->P0_2 = UART0_PIN_TXD;						/* UART0 pins on */
    LPC_IOCON->P0_3 = UART0_PIN_RXD;
    /* enable & reset fifo */
    LPC_UART0->FCR = FCR_FIFO_ENABLE | FCR_FIFO_RST_RX | FCR_FIFO_RST_TX | FCR_RX_FIFO_TRIGGER_LVL_1CHAR;
}

/*------------------------------------------------------------------------------
 function name:		uart2Init
 description: 		init uart 2
 parameters:   		baud
 returned value:	none
------------------------------------------------------------------------------*/
void uart2Init    (enum uartBaud baud)
{
	uint32 div;

	/* turn on UART2 */
	LPC_SC->PCONP |= LPC_SC_PCONP_PCUART2;
    /* set DLAB to enable access to divisor latches- DLL and DLM are accessible,
     * 8 bit character, 1 bit stop, none parity */
    LPC_UART2->LCR = LCR_WORD_LENGTH_SELECT_8BIT /*| uartConf->stopBit | uartConf->parity*/ | LCR_DLAB;
    /* set divisor latches DLL, DLM */
    div = (SYSTEM_FREQUENCY / UART0_BASE_DIV) / baud;
    LPC_UART2->DLM = div / 256;
    LPC_UART2->DLL = div % 256;
    /* clear DLAB to disable DLL, DLM- now RBR, THR and IER are accessible */
    LPC_UART2->LCR &= ~LCR_DLAB;
    /* uart pins */
    LPC_IOCON->P0_10 = 0;									/* clear pins */
    LPC_IOCON->P0_11 = 0;
    LPC_IOCON->P0_10 = UART2_PIN_TXD;						/* turn pins on */
    LPC_IOCON->P0_11 = UART2_PIN_RXD;
    /* enable & reset fifo */
    LPC_UART2->FCR = FCR_FIFO_ENABLE | FCR_FIFO_RST_RX | FCR_FIFO_RST_TX | FCR_RX_FIFO_TRIGGER_LVL_1CHAR;
}

/*------------------------------------------------------------------------------
 function name:		uart0PutChar
 description:		function used by uprintf(), sends a char to the uart
 parameters:		character to send
 returned value:	none
------------------------------------------------------------------------------*/
void uart0PutChar (char character)
{
	while (!(LPC_UART0->LSR & LSR_THR_EMPTY));
	LPC_UART0->THR = character;
}

/*------------------------------------------------------------------------------
 function name:		uart2PutChar
 description:		function used by uprintf(), sends a char to the uart
 parameters:		character to send
 returned value:	none
------------------------------------------------------------------------------*/
void uart2PutChar (char character)
{
	while (!(LPC_UART2->LSR & LSR_THR_EMPTY));
	LPC_UART2->THR = character;
}
/*==============================================================================
 Static function definitions
==============================================================================*/

/*==============================================================================
 ISR definitions
==============================================================================*/


/******************************************************************************
* END OF FILE
******************************************************************************/



